home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 465 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: solon.com!not-for-mail
  2. From: Paul Roub <proub@shadow.net>
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Leading and Trailing Blanks
  5. Date: 5 Jan 1996 09:53:15 -0600
  6. Organization: Crash Basket
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4cjhhb$f8s@solutions.solon.com>
  10. References: <4chh1b$685@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Mailer: Mozilla 2.0b4 (Win95; I)
  13.  
  14.  
  15. Casey Claiborne wrote:
  16. > Hello -
  17. >         I am wondering if anyone out there has a program (or knows of one)
  18. > that allows one to strip leading and trailing blanks from a string.
  19. > ex:
  20.  
  21. Here's the way I do it.  This is a bit verbose, but I was going for 
  22. clarity, not conciseness.  Use it along the lines of:
  23.  
  24.    char   test[ 10 ];
  25.  
  26.    strcpy( test, "  test  " );
  27.    lrtrim( test );
  28.  
  29. [Replaced //foo\n with /*foo */\n in entire document, for C content. -mod]
  30.  
  31.    /* test now contains "test" */
  32.  
  33. /* Function:     void lrtrim() */
  34. /* */
  35. /* Description:  remove leading and trailing blanks from a        */
  36. /*               string, in place all leading and trailing        */
  37. /*               whitespace is removed                            */
  38. /* */
  39. /* Parameters:   char *st - string to trim */
  40. /* */
  41. void lrtrim( char *st )
  42. {
  43. {
  44.   /* pointer to last non-space char seen */
  45.   /* */
  46.   char     *lastNonBlank = st - 1;
  47.  
  48.  
  49.   /* pointer to left-trimmed string */
  50.   /* */
  51.   char     *newString;
  52.  
  53.   /* pointer to original string */
  54.   /* */
  55.   char     *oldString;              
  56.  
  57.   /*  initially, assume we're starting at the beginning */
  58.   /* */
  59.   newString = oldString = st;
  60.  
  61.   /*  skip leading blanks to find the new starting char */
  62.   /* */
  63.   while (isspace( *oldString ))
  64.     ++oldString;
  65.  
  66.   /*  now copy all characters, starting from the first non-blank,  */
  67.   /*  to the beginning of the string.  e.g., if the string was "  */
  68.   /*  test", then newString would be pointing at the first 't',   */
  69.   /*  while oldString would be pointing at the first space        */
  70.   /*                                                              */
  71.   /*  additionally, always keep track of the location (in the new  */
  72.   /*  string) of the last non-space character seen.  initially,   */
  73.   /*  this is set to one char before the beginning of the string, */
  74.   /*  since we haven't seen any yet                               */
  75.   /* */
  76.   while (*oldString != '\0')
  77.   {
  78.     if (! isspace( *oldString ))          /* non-space? */
  79.       lastNonBlank = newString;           /* note it */
  80.  
  81.     *newString++ = *oldString++;          /* move the char */
  82.   }
  83.  
  84.   /*  the last non-blank should be our last character.  put a 0   */
  85.   /*  terminator *after* it.  if we never saw a non-blank, this   */
  86.   /*  puts the term at the first position of the string -- just   */
  87.   /*  what we want                                                */
  88.   /* */
  89.   lastNonBlank[ 1 ] = '\0';
  90.  
  91.   return;
  92. }
  93. }
  94.  
  95.  
  96.  
  97. -paul
  98.